home *** CD-ROM | disk | FTP | other *** search
/ Micromanía 92 / CDMM92_1.ISO / SOF 2 SDK / sof2sdk-101.msi / _92D6AC311BB48EBA344BBABC89DA6AB0 / _81A94E6EB599474B9039F73A54FE7722 < prev    next >
Encoding:
Text File  |  2002-06-05  |  6.1 KB  |  292 lines

  1. // Copyright (C) 2001-2002 Raven Software
  2. //
  3. // gameinfo.c
  4. #include "ui_local.h"
  5.  
  6.  
  7. //
  8. // arena and bot info
  9. //
  10.  
  11.  
  12. int                ui_numBots;
  13. static char        *ui_botInfos[MAX_BOTS];
  14.  
  15. static int        ui_numArenas;
  16. static char        *ui_arenaInfos[MAX_ARENAS];
  17.  
  18. /*
  19. ===============
  20. UI_ParseInfos
  21. ===============
  22. */
  23. int UI_ParseInfos( const char *buf, int max, char *infos[] ) {
  24.     char    *token;
  25.     int        count;
  26.     char    key[MAX_TOKEN_CHARS];
  27.     char    info[MAX_INFO_STRING];
  28.  
  29.     count = 0;
  30.  
  31.     while ( 1 ) {
  32.         token = COM_Parse( &buf );
  33.         if ( !token[0] ) {
  34.             break;
  35.         }
  36.         if ( strcmp( token, "{" ) ) {
  37.             Com_Printf( "Missing { in info file\n" );
  38.             break;
  39.         }
  40.  
  41.         if ( count == max ) {
  42.             Com_Printf( "Max infos exceeded\n" );
  43.             break;
  44.         }
  45.  
  46.         info[0] = '\0';
  47.         while ( 1 ) {
  48.             token = COM_ParseExt( &buf, qtrue );
  49.             if ( !token[0] ) {
  50.                 Com_Printf( "Unexpected end of info file\n" );
  51.                 break;
  52.             }
  53.             if ( !strcmp( token, "}" ) ) {
  54.                 break;
  55.             }
  56.             Q_strncpyz( key, token, sizeof( key ) );
  57.  
  58.             token = COM_ParseExt( &buf, qfalse );
  59.             if ( !token[0] ) {
  60.                 strcpy( token, "<NULL>" );
  61.             }
  62.             Info_SetValueForKey( info, key, token );
  63.         }
  64.         //NOTE: extra space for arena number
  65.         infos[count] = trap_VM_LocalAlloc(strlen(info) + strlen("\\num\\") + strlen(va("%d", MAX_ARENAS)) + 1);
  66.         if (infos[count]) {
  67.             strcpy(infos[count], info);
  68.             count++;
  69.         }
  70.     }
  71.     return count;
  72. }
  73.  
  74. /*
  75. ===============
  76. UI_LoadArenasFromFile
  77. ===============
  78. */
  79. static void UI_LoadArenasFromFile( char *filename ) {
  80.     int                len;
  81.     fileHandle_t    f;
  82.     char            buf[MAX_ARENAS_TEXT];
  83.  
  84.     len = trap_FS_FOpenFile( filename, &f, FS_READ );
  85.     if ( !f ) {
  86.         trap_Print( va( S_COLOR_RED "file not found: %s\n", filename ) );
  87.         return;
  88.     }
  89.     if ( len >= MAX_ARENAS_TEXT ) {
  90.         trap_Print( va( S_COLOR_RED "file too large: %s is %i, max allowed is %i", filename, len, MAX_ARENAS_TEXT ) );
  91.         trap_FS_FCloseFile( f );
  92.         return;
  93.     }
  94.  
  95.     trap_FS_Read( buf, len, f );
  96.     buf[len] = 0;
  97.     trap_FS_FCloseFile( f );
  98.  
  99.     ui_numArenas += UI_ParseInfos( buf, MAX_ARENAS - ui_numArenas, &ui_arenaInfos[ui_numArenas] );
  100. }
  101.  
  102. /*
  103. ===============
  104. UI_LoadArenas
  105. ===============
  106. */
  107. void UI_LoadArenas( void ) 
  108. {
  109.     int            numdirs;
  110.     char        filename[128];
  111.     char        dirlist[1024];
  112.     char        *dirptr;
  113.     int            i;
  114.     int            n;
  115.     int            dirlen;
  116.     char        *type;
  117.  
  118.     ui_numArenas = 0;
  119.     uiInfo.mapCount = 0;
  120.  
  121.     // get all arenas from .arena files
  122.     numdirs = trap_FS_GetFileList("scripts", ".arena", dirlist, 1024 );
  123.     dirptr  = dirlist;
  124.     for (i = 0; i < numdirs; i++, dirptr += dirlen+1) 
  125.     {
  126.         dirlen = strlen(dirptr);
  127.         strcpy(filename, "scripts/");
  128.         strcat(filename, dirptr);
  129.         UI_LoadArenasFromFile(filename);
  130.     }
  131.     
  132. #ifdef _DEBUG
  133.     Com_Printf ( "%i arenas parsed\n", ui_numArenas );
  134. #endif
  135.  
  136.     for( n = 0; n < ui_numArenas; n++ ) 
  137.     {
  138.         // determine type
  139.         uiInfo.mapList[uiInfo.mapCount].cinematic = -1;
  140.         uiInfo.mapList[uiInfo.mapCount].mapLoadName = String_Alloc(Info_ValueForKey(ui_arenaInfos[n], "map"));
  141.         uiInfo.mapList[uiInfo.mapCount].mapName = String_Alloc(Info_ValueForKey(ui_arenaInfos[n], "longname"));
  142.         uiInfo.mapList[uiInfo.mapCount].levelShot = -1;
  143.         uiInfo.mapList[uiInfo.mapCount].imageName = String_Alloc(va("gfx/menus/levelshots/%s", uiInfo.mapList[uiInfo.mapCount].mapLoadName));
  144.         uiInfo.mapList[uiInfo.mapCount].typeBits = 0;
  145.  
  146.         type = Info_ValueForKey( ui_arenaInfos[n], "type" );
  147.  
  148.         // if no type specified, it will be treated as all
  149.         if( *type ) 
  150.         {
  151.             int gametype;
  152.  
  153.             for ( gametype = 0; gametype < bg_gametypeCount; gametype ++ )
  154.             {
  155.                 if ( strstr ( type, bg_gametypeData[gametype].name ) )
  156.                 {
  157.                     uiInfo.mapList[uiInfo.mapCount].typeBits |= (1 << gametype);
  158.                 }
  159.             }        
  160.         } 
  161.         else 
  162.         {
  163.             uiInfo.mapList[uiInfo.mapCount].typeBits = 0xFFFFFFFF;
  164.         }
  165.  
  166.         uiInfo.mapCount++;
  167.         if (uiInfo.mapCount >= MAX_MAPS) 
  168.         {
  169.             break;
  170.         }
  171.     }
  172. }
  173.  
  174.  
  175. /*
  176. ===============
  177. UI_LoadBotsFromFile
  178. ===============
  179. */
  180. static void UI_LoadBotsFromFile( char *filename ) 
  181. {
  182.     int                len;
  183.     fileHandle_t    f;
  184.     char            buf[MAX_BOTS_TEXT];
  185.  
  186.     len = trap_FS_FOpenFile( filename, &f, FS_READ );
  187.     if ( !f ) {
  188.         trap_Print( va( S_COLOR_RED "file not found: %s\n", filename ) );
  189.         return;
  190.     }
  191.     if ( len >= MAX_BOTS_TEXT ) {
  192.         trap_Print( va( S_COLOR_RED "file too large: %s is %i, max allowed is %i", filename, len, MAX_BOTS_TEXT ) );
  193.         trap_FS_FCloseFile( f );
  194.         return;
  195.     }
  196.  
  197.     trap_FS_Read( buf, len, f );
  198.     buf[len] = 0;
  199.     trap_FS_FCloseFile( f );
  200.  
  201.     COM_Compress(buf);
  202.  
  203.     ui_numBots += UI_ParseInfos( buf, MAX_BOTS - ui_numBots, &ui_botInfos[ui_numBots] );
  204. }
  205.  
  206. /*
  207. ===============
  208. UI_LoadBots
  209. ===============
  210. */
  211. void UI_LoadBots( void ) 
  212. {
  213.     vmCvar_t    botsFile;
  214.     int            numdirs;
  215.     char        filename[128];
  216.     char        dirlist[1024];
  217.     char*        dirptr;
  218.     int            i;
  219.     int            dirlen;
  220.  
  221.     ui_numBots = 0;
  222.  
  223.     trap_Cvar_Register( &botsFile, "g_botsFile", "", CVAR_INIT|CVAR_ROM, 0.0, 0.0 );
  224.     if( *botsFile.string ) 
  225.     {
  226.         UI_LoadBotsFromFile(botsFile.string);
  227.     }
  228.     else 
  229.     {
  230.         UI_LoadBotsFromFile("botfiles/bots.txt");
  231.     }
  232.  
  233.     // get all bots from .bot files
  234.     numdirs = trap_FS_GetFileList("scripts", ".bot", dirlist, 1024 );
  235.     dirptr  = dirlist;
  236.     for (i = 0; i < numdirs; i++, dirptr += dirlen+1) 
  237.     {
  238.         dirlen = strlen(dirptr);
  239.         strcpy(filename, "scripts/");
  240.         strcat(filename, dirptr);
  241.         UI_LoadBotsFromFile(filename);
  242.     }
  243.     trap_Print( va( "%i bots parsed\n", ui_numBots ) );
  244. }
  245.  
  246.  
  247. /*
  248. ===============
  249. UI_GetBotInfoByNumber
  250. ===============
  251. */
  252. char *UI_GetBotInfoByNumber( int num ) {
  253.     if( num < 0 || num >= ui_numBots ) {
  254.         trap_Print( va( S_COLOR_RED "Invalid bot number: %i\n", num ) );
  255.         return NULL;
  256.     }
  257.     return ui_botInfos[num];
  258. }
  259.  
  260.  
  261. /*
  262. ===============
  263. UI_GetBotInfoByName
  264. ===============
  265. */
  266. char *UI_GetBotInfoByName( const char *name ) {
  267.     int        n;
  268.     char    *value;
  269.  
  270.     for ( n = 0; n < ui_numBots ; n++ ) {
  271.         value = Info_ValueForKey( ui_botInfos[n], "name" );
  272.         if ( !Q_stricmp( value, name ) ) {
  273.             return ui_botInfos[n];
  274.         }
  275.     }
  276.  
  277.     return NULL;
  278. }
  279.  
  280. int UI_GetNumBots() {
  281.     return ui_numBots;
  282. }
  283.  
  284.  
  285. char *UI_GetBotNameByNumber( int num ) {
  286.     char *info = UI_GetBotInfoByNumber(num);
  287.     if (info) {
  288.         return Info_ValueForKey( info, "name" );
  289.     }
  290.     return "Sarge";
  291. }
  292.